using System.Collections.Generic;
using System.Management;
using System.Utility.System;

namespace System.Utility.Win32
{
	public class WMI
	{
		public WMI()
		{
		}

		private static ManagementObjectCollection ExecuteCommand(string command)
		{
			Guard.ArgumentNotNullOrEmpty(command, "command");
			return (new ManagementClass(command)).GetInstances();
		}

		private static ManagementObjectCollection ExecuteSearch(string path)
		{
			Guard.ArgumentNotNullOrEmpty(path, "path");
			return (new ManagementObjectSearcher(path)).Get();
		}

		public static ManagementObjectCollection FindUSBDevicePartitions(string serialNumber)
		{
			ManagementObjectCollection managementObjectCollections;
			foreach (ManagementBaseObject managementBaseObject in ExecuteSearch("select * from Win32_DiskDrive where InterfaceType='USB' "))
			{
				Print(managementBaseObject);
				string str = string.Concat("ASSOCIATORS OF {Win32_DiskDrive.DeviceID='", managementBaseObject["DeviceID"], "'} WHERE AssocClass = Win32_DiskDriveToDiskPartition");
				ManagementObjectCollection.ManagementObjectEnumerator enumerator = ExecuteSearch(str).GetEnumerator();
				try
				{
					if (enumerator.MoveNext())
					{
						ManagementBaseObject current = enumerator.Current;
						string str1 = string.Concat("ASSOCIATORS OF {Win32_DiskPartition.DeviceID='", current["DeviceID"], "'} WHERE AssocClass =  Win32_LogicalDiskToPartition");
						managementObjectCollections = ExecuteSearch(str1);
						return managementObjectCollections;
					}
				}
				finally
				{
					if (enumerator != null)
					{
						((IDisposable)enumerator).Dispose();
					}
				}
			}
			managementObjectCollections = null;
			return managementObjectCollections;
		}

		public static string FindUSBDevicesPartition(string serialNumber)
		{
			string safeString;
			ManagementObjectCollection managementObjectCollections = FindUSBDevicePartitions(serialNumber);
			if ((managementObjectCollections == null ? false : managementObjectCollections.Count > 0))
			{
				ManagementObjectCollection.ManagementObjectEnumerator enumerator = managementObjectCollections.GetEnumerator();
				try
				{
					if (enumerator.MoveNext())
					{
						safeString = TypeExtension.ToSafeString(enumerator.Current["Name"]);
						return safeString;
					}
				}
				finally
				{
					if (enumerator != null)
					{
						((IDisposable)enumerator).Dispose();
					}
				}
				safeString = string.Empty;
			}
			else
			{
				safeString = string.Empty;
			}
			return safeString;
		}

		public static List<DirverInfo> GetDirverInfo()
		{
			ManagementObjectCollection managementObjectCollections = ExecuteSearch("select * from win32_logicaldisk");
			List<DirverInfo> dirverInfos = new List<DirverInfo>();
			foreach (ManagementBaseObject managementBaseObject in managementObjectCollections)
			{
				DirverInfo str = new DirverInfo();
				try
				{
					str.Name = managementBaseObject["Name"].ToString();
					str.VolumeName = managementBaseObject["VolumeName"].ToString();
					str.Size = TypeExtension.ToInt64(managementBaseObject["Size"].ToString());
					str.FreeSize = TypeExtension.ToInt64(managementBaseObject["FreeSpace"].ToString());
				}
				catch
				{
					str.VolumeName = "设备未准备好";
				}
				try
				{
					str.Type = TypeExtension.ToEnumByValue<EnumDirverType>(managementBaseObject["DriveType"]);
				}
				catch
				{
					str.Type = EnumDirverType.Unknow;
				}
				dirverInfos.Add(str);
			}
			return dirverInfos;
		}

		public static OperatingSystem GetOperatingSystem()
		{
			OperatingSystem operatingSystem;
			ManagementObjectCollection managementObjectCollections = ExecuteCommand("Win32_OperatingSystem");
			OperatingSystem str = new OperatingSystem();
			ManagementObjectCollection.ManagementObjectEnumerator enumerator = managementObjectCollections.GetEnumerator();
			try
			{
				if (enumerator.MoveNext())
				{
					ManagementBaseObject current = enumerator.Current;
					str.System = current["Caption"].ToString();
					str.Version = current["Version"].ToString();
					str.Manufacturer = current["Manufacturer"].ToString();
					str.CSName = current["csname"].ToString();
					str.WindowsDirectory = current["WindowsDirectory"].ToString();
					operatingSystem = str;
					return operatingSystem;
				}
			}
			finally
			{
				if (enumerator != null)
				{
					((IDisposable)enumerator).Dispose();
				}
			}
			operatingSystem = str;
			return operatingSystem;
		}

		private static void Print(ManagementBaseObject obj)
		{
			foreach (PropertyData property in obj.Properties)
			{
				object[] name = new object[] { "Name: ", property.Name, "\t Value: ", property.Value };
				Console.WriteLine(string.Concat(name));
			}
		}
	}
}